

local Mino = require("client.tetris.mino")


local Game = class("Game")

function Game:ctor()
    self.super:ctor()

    self.grid = new("client.tetris.grid")
    self.current=nil
    self.next=nil
    self.level=1
    self.fallSpeed=1
    self.levelSpeed=45*1000
    self.fallTime=0
    self.levelTime=0
    self.pauseTime=0
    self.isGameOver=false
    
    self.items = {}

    self:onStart()
    self:register()
end

function Game:register()

end

function Game:onStart()
    table.insert(self.items,{tetrimino = "Z", sprId = g.service.const.RED_CELL_SPR})
    table.insert(self.items,{tetrimino = "S", sprId = g.service.const.YELLOW_CELL_SPR})
    table.insert(self.items,{tetrimino = "J", sprId = g.service.const.DARKBLUE_CELL_SPR})
    table.insert(self.items,{tetrimino = "T", sprId = g.service.const.BLUE_CELL_SPR})
    table.insert(self.items,{tetrimino = "O", sprId = g.service.const.BROWN_CELL_SPR})
    table.insert(self.items,{tetrimino = "I", sprId = g.service.const.LIGHTBLUE_CELL_SPR})
    

    
    self.grid:Draw(0,0)
    self:reset()
    self:spawn()
    self.fallTime = os.time()

    if self.pauseTime>0 then
        local passTime = self.levelTime - self.pauseTime
        self.levelTime = os.time() + passTime
    end

    self.pauseTime=0

    g.fork(function()
        while true do
            g.sleep(100)
            self:update()
        end
    end)

end

function Game:getMino()
    local item = self.items[math.random(#self.items)]
    local mino = Mino:new()
    return mino:create(item.tetrimino, sprId)
end

function Game:spawn()
    --g.log:info("spawn")
    if self.current==nil and not self.grid.isAnim then
        self.current = self.next
        self.current:move(3,0)
        self.fallTime = os.time()
        self.next = self:getMino()
    end
end

function Game:reset()
    self.next =	self:getMino()
    self.current = nil
    self.levelTime = os.time()
    self.fallTime = os.time()
    --playerScore=0
    self.level=1
    self.fallSpeed=1
    self.isGameOver=false
end


function Game:update()
    --completion()
    self:spawn()

    if self.isGameOver then
        self:onGameOver()
        return
    end

    --if btnp(5) then onPause() end

    --if self.current ~= nil then
    --    if btnp(4,10,10) or btnp(0,10,10) then self.current.DoRotation(true,self.grid) end
    --    if btnp(1,5,5) then self.current.GoDown(self.grid) end
    --    if btnp(2,5,5) then self.current.GoLeft(self.grid) end
    --    if btnp(3,5,5) then self.current.GoRight(self.grid) end
    --end

    --self.grid:AnimationUpdate()
    self:fall()
end

function Game:fall()
    local now = os.time()
    if self.current~=nil and self.fallTime+self.fallSpeed < now then
        self.fallTime = now
        self.current:GoDown(self.grid)
    end

    if self.levelTime+self.levelSpeed < now then
        self.levelTime = now
        self:levelUp()
    end
end






return Game